<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
         <el-form-item label="职工">
          <check-emp :itemId="searchForm.empid" @checkedItem="searchCheckedEmp"/>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="dataTime" type="daterange" range-separator="~" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addData">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="salaryid" label="流水号"></el-table-column> -->
      <el-table-column prop="empname" label="职工名称"></el-table-column>
      <el-table-column prop="salary" label="工资金额"></el-table-column>
      <el-table-column prop="salaryinfo" label="明细" show-overflow-tooltip></el-table-column>
      <el-table-column prop="posttime" label="发放时间"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="salaryimg" label="图片">
        <template slot-scope="scop">
          <el-image fit="cover" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.salaryimg"
            :preview-src-list="[$Url + '/upload/' + scop.row.salaryimg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <!-- <el-button type="primary" icon="el-icon-edit" circle></el-button> -->
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.salaryid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增工资信息" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="addForm" ref="addSetForm" :rules="rule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="职工" prop="empid">
          <check-emp :itemId="addForm.empid" @checkedItem="checkedEmp"/>
        </el-form-item>
        <el-form-item label="工资金额" prop="salary">
          <el-input v-model="addForm.salary" placeholder="请输入支出金额" clear></el-input>
        </el-form-item>
        <el-form-item label="发放时间" prop="posttime">
          <el-date-picker v-model="addForm.posttime" type="datetime" placeholder="请选择支出时间" value-format="yyyy-MM-dd hh:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工资明细" prop="salaryinfo">
          <el-input v-model="addForm.salaryinfo" placeholder="请输入工资明细" clear></el-input>
        </el-form-item>
        <el-form-item label="工资凭证">
          <upload v-model="addForm.salaryimg" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findSalary, addSalary, delSalary } from "@/api/user";
import checkEmp from "@/views/components/checkEmp";
import upload from "@/components/uploadFile";
export default {
  components:{
    checkEmp,
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      dialogFormVisible: false,
      dataTime:[],
      searchForm: {
        empid: "",
        begintime: "",
        endtime: ""
      },
      addForm: {
        empid: '',
        salary: '',
        salaryinfo: '',
        salaryimg: '',
        posttime: ''
      },
      rule: {
        empid: [
          { required: true, message: '请选择职工', trigger: 'change' }
        ],
        salary: [
          { required: true, message: '请填写工资金额', trigger: 'change' }
        ],
        salaryinfo: [
          { required: true, message: '请填写工资明细', trigger: 'change' }
        ],
        posttime: [
          { required: true, message: '请选择发放时间', trigger: 'change' }
        ]
      }
    };
  },
  watch: {
    dataTime(n) {
      console.log(n);
      if (n) {
        this.searchForm.begintime = n[0]
        this.searchForm.endtime = n[1]
      } else {
        this.searchForm.begintime = ''
        this.searchForm.endtime = ''
      }
    }
  },
  computed: {
    queryData() {
      const {empid, begintime, endtime} = this.searchForm
      return {
        empid,
        begintime,
        endtime,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findSalary.post(this.queryData);
      console.log(res, 555);
      if (res.resultCode === "000000") {
        this.tableData = res.data.salaryList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    checkedEmp(val){
      this.addForm.empid = val
    },
    searchCheckedEmp(val){
      this.searchForm.empid = val
    },
    addData(){
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addSetForm'].resetFields();
      })
      this.addForm = {
        empid: '',
        salary: '',
        salaryinfo: '',
        salaryimg: '',
        posttime: ''
      }
    },
    addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (valid) {
          const res = await addSalary.post(this.addForm);
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.dialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delSalary.post({ salaryid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
